package com.lims.core.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javacommon.base.BaseManager;
import javacommon.base.EntityDao;
import javacommon.base.ListRange;
import javacommon.util.AdapterSql;
import javacommon.util.TextTool;

import cn.org.rapid_framework.page.Page;

import com.lims.admin.model.User;
import com.lims.landInfo.dao.LandDao;
import com.lims.landInfo.model.Land;
@Service @SuppressWarnings("unchecked")
public class LandInfoManager extends BaseManager<Land,java.lang.String>{

	private LandDao landDao;
	/**增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,请注意大小写*/
	public void setLandDao(LandDao dao) {
		this.landDao = dao;
	}
	
	public EntityDao getEntityDao() {
		return this.landDao;
	}
	
	/*****
	 * jqueryEasyui Dwr 调用  需要验权限
	 * @param map
	 * @param corpno
	 * @param session
	 * @return
	 * update 2013-07-24  修改为了Action返回JSON
	 */
	@Transactional(readOnly=true)
	public ListRange findLandByCorp(String corpno,HttpSession session,Page page) {
		//验证权限
		ListRange range = new ListRange(); 
		Object obj = session.getAttribute("user");
		if(obj==null) return range;
		User u = (User)obj;
		//String corpno = map.get("corpno").toString();
		if(!(u.getCorpnoList().contains(corpno))) return null;
		//取所有地块
		List<String> cnoList = new ArrayList<String>();
		cnoList.add(corpno);
		this.findCorpnoList(cnoList,corpno);
		StringBuffer sb = new StringBuffer();
		for (Iterator iterator = cnoList.iterator(); iterator.hasNext();) {
			String cno = (String) iterator.next();
			sb.append("'"+cno+"'");
			if(iterator.hasNext()){
				sb.append(",");
			}
		}
		String sql = AdapterSql.GetSQL("findLandByCorpno",sb.toString());
		Page p = new Page(1,10,100);
		try {
			p = this.landDao.findBySql(sql, page);
		} catch (Exception e) {
			e.printStackTrace();
		}
		range.setTotal(p.getTotalCount());
		range.setRows(p.getResult().toArray());
		return range;
	}

	/**
	 * 进入进图前调用取得要查询所有地块信息   已验证过权限
	 * @param corpno
	 * @return List
	 */
	@Transactional(readOnly=true)
	public List findLandByCorpno(String corpno) {
		//取得所有单位的编号
		List<String> corpnoList = new ArrayList<String>(0);
		corpnoList.add(corpno);
		findCorpnoList(corpnoList,corpno);
		//取出查询单位下的所有地块
		StringBuffer sb = new StringBuffer();
		for (Iterator iterator = corpnoList.iterator(); iterator.hasNext();) {
			String cno = (String) iterator.next();
			sb.append("'"+cno+"'");
			if(iterator.hasNext()){
				sb.append(",");
			}
		}
		String sql = AdapterSql.GetSQL("findLandMapByCorpno",sb.toString());
		Page page = this.landDao.findBySql(sql, null);
		return page.getResult();
	}
	
	//循环取得所有单位编号
	@Transactional(readOnly=true)
	private void findCorpnoList(List<String> corpnoList, String currCorpno) {
		String hql = "select c.corpno from Corpaff c where c.parentno=?";
		List list = this.landDao.find(hql, currCorpno);
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			String corpno = (String) iterator.next();
			corpnoList.add(corpno);
			findCorpnoList(corpnoList,corpno);
		}
	}
	/*****根据宗地编号取得所有信息******/
	@Transactional(readOnly=true)
	public Map findLandAllByLandno(String landno) {
		String[] hql = new String[]{
			"from Land l where l.landno='"+landno+"'",
			"from HouseRent l where l.landno='"+landno+"'",
			"from Guarantee l where l.landno='"+landno+"'",
			"from Lawsuit l where l.landno='"+landno+"'",
			"from Logout l where l.landno='"+landno+"'",
			"from Book l where l.landno='"+landno+"'",
			"from LandUse l where l.landno='"+landno+"'",
			"from House l where l.landno='"+landno+"'",
			"from RealEstate l where l.landno='"+landno+"'",
			"from Project l where l.landno='"+landno+"'"
		};
		Map<String, Object> map = new HashMap<String, Object>();
		for (int i = 0; i < hql.length; i++) {
			List list = this.landDao.findByHQL(hql[i]);
			map.put(""+i, TextTool.isNull(list) ? null : list.get(0));
		}
		return map;
	}

	
	/**
	 * 根据土地编号取得 图片
	 * @param landno
	 * @param fileType
	 * @return
	 */
	public List findLandFileByLandAndType(String landno, int fileType) {
		String hql = "from LandFile lf where lf.land.landno='"+landno+"' and lf.fileType="+fileType;
		return this.landDao.findByHQL(hql);
	}
	
	
}
